package com.yycx.module.bus.provider.controller.hy;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bus.provider.service.hy.HyRemoteOrderService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 出订单表控制器
 *
 * @author flyme
 * @date 2020-11-28
 */
@RestController
@RequestMapping("/hy/remote/out/order/")
public class HyRemoteOutOrderController {

    @Autowired
    private HyRemoteOrderService hyRemoteOrderService;
    @Autowired
    private SysCompanyService companyService;


    @ApiOperation(value = "非两票订单-分页列表", notes = "非两票分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        String dbName = getDbName();
        return hyRemoteOrderService.outOrderPageList(dbName, params);
    }


    @ApiOperation(value = "非两票订单-详情", notes = "非两票订单详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam String orderNo) {
        String dbName = getDbName();
        EntityMap result = hyRemoteOrderService.getOutOrderById(dbName, orderNo);
        return ResultBody.ok(result);
    }

    @ApiOperation(value = "海鹰订单-列表", notes = "海鹰订单列表")
    @GetMapping(value = "queryOutHyOrderDetailList")
    public ResultBody queryHyOrderDetailList(String orderNo) {
        String dbName = getDbName();
        List<EntityMap> list = hyRemoteOrderService.queryOutHyOrderDetailList(dbName, orderNo);
        return ResultBody.ok(list);
    }

    private String getDbName() {
        OpenUser openUser = OpenHelper.getUser();
        Long companyId = openUser.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String dbName = "";
        if (FlymeUtils.isNotEmpty(company)) {
            dbName = company.getBusinessScope();
            ApiAssert.isNotEmpty("账套实例未配置", dbName);
        }
        return dbName;
    }
}
